package com.example;

import com.example.service.UserService;
import com.example.zkconnect.ZkConnection;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.data.Stat;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

/**
 * @Description
 * @Author 26233
 * @Create 2021-11-02 18:05:01
 */

public class Main {

    public static void main(String[] args) throws KeeperException, InterruptedException, RemoteException, NotBoundException, MalformedURLException {
        YamlParser yamlParser = new YamlParser("/config.yaml");
        String zkAddress = yamlParser.getZkAddress();

        ZkConnection zkConnection = new ZkConnection(zkAddress, 60);
        byte[] rmiUrlByte = zkConnection.getConnect().getData("/myservice/" + UserService.class.getName() + "Impl", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {

            }
        }, new Stat());


        String rmiUrl = new String(rmiUrlByte);
        System.out.println("rmiUrl = " + rmiUrl);
        UserService service = (UserService) Naming.lookup(rmiUrl);
        String user = service.findUser("123456");
        System.out.println("user = " + user);
    }

}
